package com.example.gamestore.controller;

import com.example.gamestore.pojo.Game;
import com.example.gamestore.service.GameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zihao
 * @date 2025/6/6 19:03
 * @VERSION
 * @description
 **/
@CrossOrigin//前后端允许跨域
@Slf4j //日志对象,用来代替我们写的各种输出语句
@Controller
public class GameController {

    @Autowired
    private GameService gameService;

    @GetMapping("/game/query")
    public String query(Model model) {
        List<Game> games = gameService.query();
        model.addAttribute("games", games);
        return "game/query";
    }

    @PostMapping("/game/update")
    public String updateGame(@RequestBody Game game, Model model) {
        log.info("Received game object for update: {}", game);
        int result = gameService.updateGame(game);
        if (result > 0) {
            // 修改成功，重定向到用户管理页面
            return "redirect:/home";
        } else {
            // 修改失败，返回修改页面并显示错误信息
            model.addAttribute("error", "游戏修改失败");
            return "gameupdate";
        }
    }

    @PostMapping("/game/add")
    public String add(@RequestBody Game game) {
        Game addedGame = gameService.add(game);

        if (addedGame != null) {
            return "redirect:/game/query"; // 重定向到游戏列表页面
        } else {
            return "add"; // 返回 add.html 模板
        }
    }

    // 游戏删除
    @GetMapping("/game-delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            gameService.delete(id);
            result.put("success", true);
            result.put("message", "游戏删除成功");
        } catch (Exception e) {
            log.error("删除游戏失败，游戏 ID: {}", id, e);
            result.put("success", false);
            result.put("message", "游戏删除失败，可能有相关联的购物车记录");
        }
        return result;
    }

    // 显示游戏详情页面
    @GetMapping("/game/{id}")
    public String showGameDetail(@PathVariable Long id, Model model) {
        Game game = gameService.getGameById(id);
        if (game == null) {
            return "redirect:/"; // 如果游戏不存在，重定向到首页
        }

        // 获取相关游戏（这里简单实现，实际应用中可能需要更复杂的逻辑）
        List<Game> relatedGames = gameService.getRelatedGames(id, 4);

        model.addAttribute("game", game);
        model.addAttribute("relatedGames", relatedGames);
        return "game";
    }

    // 获取游戏详情的API（用于前端AJAX请求）
    @GetMapping("/api/game/{id}")
    @ResponseBody
    public Game getGameById(@PathVariable Long id) {
        return gameService.getGameById(id);
    }

    // 获取相关游戏的API
    @GetMapping("/api/games/related/{id}")
    @ResponseBody
    public List<Game> getRelatedGames(@PathVariable Long id, @RequestParam(defaultValue = "4") int limit) {
        return gameService.getRelatedGames(id, limit);
    }

}
